__author__ = 'Zhaokun'
#encoding=utf-8

import os

codes = {'''SET_INPUT_MODE(pGBIS, nLang, nSubLang, nInputMode)'''
: """GBSetCurLanguage(pGBIS, nLang, nSubLang);
GBSetInputMode(pGBIS, nInputMode); """,
        """SET_INPUT_STRING(pGBIS, c_inputStr)"""
: """wcscpy(pGBIS->inputString, c_inputStr);
pGBIS->inputStringLen = (GBU8)wcslen(c_inputStr);
pGBIS->flags |= GBIS_INPUT_STRING_UPDATED; """}



class SwitchDefine():
    """转换宏映射，对内容通过宏进行替换
1.首先调用ExtractionMacro对语句进行分解
2.然后调用AddMacro添加映射
3.然后进行转换
    """
    mymap = {}
    def AddMacro(self, fun, param, funContent):
        """fun 函数名
    param 参数列表
    funContent 宏内容
        """
        self.mymap[fun] = [param, funContent]
        for i in range(len(param)):
            self.mymap[fun][0][i] = self.mymap[fun][0][i].strip()
#        print self.mymap

    def ExtractionMacro(self, funDefine):
        start = funDefine.find('(')
        end = funDefine.rfind(')')
        if start == -1 or end == -1:
            return -1
        fun = funDefine[:start]
        param = funDefine[start + 1 : end].split(',')
        return fun, param

    def Switch(self, code):
        paramMap = {}
        fun, tarParams = self.ExtractionMacro(code)
        [sourParams, content] =  self.mymap[fun]
        for i in range(len(tarParams)):
            paramMap[sourParams[i]] = tarParams[i]
        content.replace('pGBIS', 'g_gbis')
        for key in paramMap.keys():
            content = content.replace(key, paramMap[key])
        return content

def SwitchFile(switch, file):
    fp = open(file, 'r')
    fpOut = open(file+'_new', 'w')
    isFind = 0
    for line in fp:
        isFind = 0
        for fun in switch.mymap.keys():
            index = line.find(fun)
            if index == -1:
                continue
            if line[index + len(fun)] != '(' or line[index - 1] in "[a-zA-Z0-9]":
                continue
            if line.find('//') >= 0 and line.find('//') < index:
                break
            isFind = 1
        if isFind == 0:
            fpOut.write(line)
            continue

        space = line.find(line.strip())
        line = line[:line.rfind(';')].strip()
        code = switch.Switch(line)
#        print code
#        print '=========================='
        code = '\t'*space + code.replace('\n', '\n' + '\t'*space) + '\r\n'
        fpOut.write(code)

def main():
    switch = SwitchDefine()
    for code in codes.keys():
        fun, param = switch.ExtractionMacro(code)
        switch.AddMacro(fun, param, codes[code])
#        print switch.Switch('SET_INPUT_STRING(g_gbis, L"hello")')
#    print switch.mymap
    path = r'E:\Project\python\pyTools\20110603'

    for file in os.listdir(path):
        if (file == r'UnitTest.c'
            or os.path.isdir(file) == True
            or file.find('_new') != -1):
            continue
        print file
        SwitchFile(switch, path + '\\' + file)
    
if __name__ == '__main__':
    path = r'E:\Project\python\pyTools\20110603'
    newdir = r'E:\Project\python\pyTools\20110603\new'

    for file in os.listdir(newdir):
        if (os.path.isdir(file) == True
            or file.find('_new') == -1):
            continue
        print file
        newfilename = file[:file.rfind('_new')]
        os.rename(newdir + '\\' + file, newdir + '\\' + newfilename)
